﻿using System.Collections.Generic;

namespace lab05
{
    interface ICommand
    {
        void Execute();
    }

    abstract class ACommand : ICommand
    {
        private ManagerCommand _manager;
        public bool isActive { get; set; }
        public ACommand(ManagerCommand manager)
        {
            _manager = manager;
        }

        public void doExecute()
        {
            if (isActive)
                _manager.Add(this);
        }

        abstract public void Execute();
    }


    class ManagerCommand : ICommand
    {
        private List<ICommand> _commands;

        public bool isActive { get; set; }

        public void Add(ICommand cmd)
        {
            _commands.Add(cmd);
        }

        public ManagerCommand()
        {
            _commands = new List<ICommand>();
            isActive = true;
        }

        public void Execute()
        {
            foreach (ACommand command in _commands)
            {
                command.isActive = isActive;
                command.Execute();
            }
        }

        public void Undo()
        {
            RemoveLast();
            isActive = false;
            Execute();
            isActive = true;
        }

        private void RemoveLast()
        {
            if (_commands.Count > 1)
            {
                _commands.RemoveRange(_commands.Count - 1, 1);
            }
            else
            {
                System.Console.Beep();
            }
        }
    }
}
